import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/entity/entities/lf_bool_entity.dart';
import 'package:largefriends/net/error_handle.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';

toDeleteAccount() {
  Get.offAndToNamed(
    LFRoute.pathDeleteAccount,
  );
}

class LfDeleteAccountLogic extends GetxController {
  final TextEditingController passwordController = TextEditingController();
  final FocusNode passwordNode = FocusNode();
  RxString password = "".obs;
  LfAuthService authService = Get.find();
  bool get enableDelete => password.value.length >= 6;

  @override
  void onClose() {
    passwordController.dispose();
    passwordNode.dispose();
    super.onClose();
  }

  deleteAccount() async {
    showLoading();
    await LfDio().post<LfBoolEntity>(url: apiDeleteAccount, params: {
      "password": password.value.trim(),
      "reason": ''
    }).then((value) {
      hideLoading();
      authService.loginOut();
    }, onError: (err) {
      hideLoading();
      if (err is NetError) {
        showSnackBar(err.msg);
      }
    });
  }
}
